package com.mt.yanyou.guide;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mt.yanyou.R;
import com.mt.yanyou.adapter.BasePagerAdapter;
import com.mt.yanyou.login.LoginActivity;
import com.mt.yanyou.test.TestActivity;
import com.mt.yanyou.utils.SharedPreferencesUtil;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class WelcomeActivity extends AppCompatActivity {
    ImageView iv_guide_point_1;
    ImageView iv_guide_point_2;
ImageView iv_guide_point_3;
TextView tv_guide_skip;
Button btn_experience;
//存放framelayout，展示引导页内容
private View view1;
private View view2;
private View view3;

    private ViewPager mViewPager;
private List<View> mPageList = new ArrayList<>();
    private BasePagerAdapter mPageAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        setContentView(R.layout.activity_welcome);
        init_view();
//     check_guide();
     button_skip();
     init_view();
        init_spot();

    }

    /**
     * 初始化view
     */
    private void init_view() {
        btn_experience = findViewById(R.id.btn_experience);
        tv_guide_skip = findViewById(R.id.tv_guide_skip);
        mViewPager = findViewById(R.id.mViewPager);
         iv_guide_point_1 = findViewById(R.id.iv_guide_point_1);
        iv_guide_point_2 = findViewById(R.id.iv_guide_point_2);
        iv_guide_point_3 = findViewById(R.id.iv_guide_point_3);
        view1 = View.inflate(this, R.layout.layout_pager_guide_1, null);
        view2 = View.inflate(this, R.layout.layout_pager_guide_2, null);
        view3 = View.inflate(this, R.layout.layout_pager_guide_3, null);
        mPageList.add(view1);
        mPageList.add(view2);
        mPageList.add(view3);
        //预加载
        mViewPager.setOffscreenPageLimit(mPageList.size());

        mPageAdapter = new BasePagerAdapter(mPageList);
        mViewPager.setAdapter(mPageAdapter);
    }

    /**
     * 点击跳过跳转
     */
    private void button_skip() {
        tv_guide_skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
            }
        });
        btn_experience .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(WelcomeActivity.this, TestActivity.class));
            }
        });

    }
    /**
     * 小圆点的逻辑
     */
public void init_spot(){
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        seletePoint(position);
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
}

    /**
     * 动态选择小圆点
     * @param position
     */
    private void seletePoint(int position) {
        switch (position){
            case 0:
                iv_guide_point_1.setImageResource(R.drawable.img_guide_point_p);
                iv_guide_point_2.setImageResource(R.drawable.img_guide_point);
                iv_guide_point_3.setImageResource(R.drawable.img_guide_point);
                break;
            case 1:
                iv_guide_point_1.setImageResource(R.drawable.img_guide_point);
                iv_guide_point_2.setImageResource(R.drawable.img_guide_point_p);
                iv_guide_point_3.setImageResource(R.drawable.img_guide_point);
                break;
            case 2:
                iv_guide_point_1.setImageResource(R.drawable.img_guide_point);
                iv_guide_point_2.setImageResource(R.drawable.img_guide_point);
                iv_guide_point_3.setImageResource(R.drawable.img_guide_point_p);
                break;
        }
    }


    /**
     * 检测app是否需要打开引导页
     */
    private void check_guide() {
        //判断app是否第一次打开
        Boolean isFirstOpen = SharedPreferencesUtil.getBoolean(WelcomeActivity.this,SharedPreferencesUtil.FIRST_OPEN,true);
        //是第一次启动
        if(isFirstOpen){
            startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
            finish();
            return;
        }else{

//跳转登录界面，同时把状态存入
        }
    }
}
